package com.oa.entities.flows;

import com.oa.common.enums.*;
import com.oa.domain.UndeleteEntity;
import com.oa.entities.User;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @name 流程的基类
 *
 */
@Entity
@Table(name = "oa_flow")
public class Flow extends UndeleteEntity<Long> {
    /**
     *
     */
    private static final long serialVersionUID = -6537644537236147454L;

    /** 流程的名称 **/
    public static final String FLOW_APPS_NAME="name";

    /** 流程公文模板的id **/
    public static final String FLOW_APPS_TYPE_ID="appTypeId";

    /** 步骤 **/
    public static final String FLOW_NUM="num";

    /** 名称 **/
    private String name;

    /** 步骤 **/
    private int num;

    /** 拟办人员 **/
    private User flowUser;

    /** 拟办的id **/
    private Long flowUserId;

    /** 发布人**/
    private User issue;

    /** 发布人的id **/
    private Long issueId;

    /** 模版 **/
    private AppType appType;

    /** 模版的id **/
    private Long appTypeId;

    /** 流程的状态 **/
    private int flowStatus;

    /** 流程的类型 **/
    private int flowType;

    /** 审核状态 **/
    private int auditType;

    /** 数据类型 **/
    private int dateType;

    /** 处理的类型 **/
    private int fileType;

    /** 表单的权限 **/
    private String roles;

    /** 下一步 **/
    private Set<Flow> flows= new HashSet<>();

    /** 上一步 **/
    private Flow parent;

    /** 流程的表单 **/
    private List<String> forms= new ArrayList<>();
    @Column(name = "flow_name")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Column(name = "flow_num")
    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    @Override
    @Id
    @Column(nullable = false,unique = true)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Long getId() {
        return super.getId();
    }

    @Override
    public void setId(Long id) {
        super.setId(id);
    }

    /**
     * 设置为正常
     */
    public void setNormal(){
        this.flowStatus= FlowStatus.NORMAL.getCode();
    }

    /**
     * 设置结束
     */
    public void setEnd(){
        this.flowStatus= FlowStatus.END.getCode();
    }

    /**
     * 设置返回
     */
    public void setReturn(){
        this.flowStatus= FlowStatus.RETURN.getCode();
    }

    /**
     * 设置为可选
     */
    public void setChooseAble(){
        this.flowType= FlowType.CHOOSABLE.getCode();
    }

    /**
     * 设置不可选
     *
     */
    public void setChooseDisable(){
        this.flowType= FlowType.CHOOSDISABLE.getCode();
    }


    /**
     * 设置不使用超时
     */
    public void setDisableTime(){
        this.dateType= DateType.DISABLE_TIMEOUT.getCode();
    }

    /**
     * 设置超时通过
     */
    public void setTimeOutPass(){
        this.dateType= DateType.TIMEOUT_PASS.getCode();
    }

    /**
     * 设置超时拒绝
     *
     */
    public void setTimeOutFail(){
        this.dateType= DateType.TIMEOUT_FAIL.getCode();
    }

    /**
     * 设置为文件阅读
     */
    public void setRead(){
        this.fileType= FlowFileType.FILE_READ.getCode();
    }

    /**
     * 设置文件分发
     */
    public void setDistribute(){
        this.fileType= FlowFileType.FILE_DISTRIBUTE.getCode();
    }

    /**
     * 设置为归档
     *
     */
    public void setPigeonhole(){
        this.fileType= FlowFileType.FILE_PIGEONHOLE.getCode();
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "flow_user_id",nullable = false,updatable = false,insertable = false)
    public User getFlowUser() {
        return flowUser;
    }

    public void setFlowUser(User flowUser) {
        this.flowUser = flowUser;
    }

    @Column(name = "flow_user_id")
    public Long getFlowUserId() {
        return flowUserId;
    }

    public void setFlowUserId(Long flowUserId) {
        this.flowUserId = flowUserId;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "issue_id",nullable = false,updatable = false,insertable = false)
    public User getIssue() {
        return issue;
    }

    public void setIssue(User issue) {
        this.issue = issue;
    }

    @Column(name = "issue_id")
    public Long getIssueId() {
        return issueId;
    }

    public void setIssueId(Long issueId) {
        this.issueId = issueId;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "app_type_id",nullable = false,updatable = false,insertable = false)
    public AppType getAppType() {
        return appType;
    }

    public void setAppType(AppType appType) {
        this.appType = appType;
    }

    @Column(name = "app_type_id")
    public Long getAppTypeId() {
        return appTypeId;
    }

    public void setAppTypeId(Long appTypeId) {
        this.appTypeId = appTypeId;
    }

    @Column(name = "flow_status",nullable = false)
    public int getFlowStatus() {
        return flowStatus;
    }

    public void setFlowStatus(int flowStatus) {
        this.flowStatus = flowStatus;
    }

    @Column(name = "flow_type",nullable = false)
    public int getFlowType() {
        return flowType;
    }

    public void setFlowType(int flowType) {
        this.flowType = flowType;
    }

    @Column(name = "audit_type",nullable = false)
    public int getAuditType() {
        return auditType;
    }

    public void setAuditType(int auditType) {
        this.auditType = auditType;
    }

    @Column(name = "date_type",nullable = false)
    public int getDateType() {
        return dateType;
    }

    public void setDateType(int dateType) {
        this.dateType = dateType;
    }

    @Column(name = "file_type")
    public int getFileType() {
        return fileType;
    }

    public void setFileType(int fileType) {
        this.fileType = fileType;
    }

    public String getRoles() {
        return roles;
    }

    public void setRoles(String roles) {
        this.roles = roles;
    }

    @OneToMany(fetch = FetchType.LAZY,cascade = CascadeType.ALL,mappedBy = "parent")
    public Set<Flow> getFlows() {
        return flows;
    }

    public void setFlows(Set<Flow> flows) {
        this.flows = flows;
    }


    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "parent_id")
    public Flow getParent() {
        return parent;
    }

    public void setParent(Flow parent) {
        this.parent = parent;
    }

    @Column(name = "form_name",length = 255,nullable = false)
    @ElementCollection(fetch = FetchType.LAZY)
    @JoinTable(name = "oa_flow_form",joinColumns = {@JoinColumn(name = "flow_id",referencedColumnName = "id")}
    ,uniqueConstraints = {@UniqueConstraint(columnNames = {"flow_id","form_name"})})
    public List<String> getForms() {
        return forms;
    }

    public void setForms(List<String> forms) {
        this.forms = forms;
    }
}
